package com.mercenary.wein.screenControl.deviceControll;

import com.android.chimpchat.ChimpManager;
import com.android.chimpchat.adb.AdbChimpDevice;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.TouchPressType;
import com.android.ddmlib.IDevice;
import com.mercenary.wein.screenControl.ui.JPanelScreen;

import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;

public class OperateAndroid {
	private AdbChimpDevice device;
	private ChimpManager manager;

    private static double deviceWidthRatio=720d/DeviceControlDecorator.jpsWidth;
    private static double deviceHeightRatio=1280d/DeviceControlDecorator.jpsHight;

    public ChimpManager getManager() {
        return manager;
    }

    public void setManager(ChimpManager manager) {
        this.manager = manager;
    }

    public AdbChimpDevice getDevice() {
        return device;
    }

    public void setDevice(AdbChimpDevice device) {
        this.device = device;
    }

    private static OperateAndroid oa;

	public static String HOME = "KEYCODE_HOME";
	public static String BACK = "KEYCODE_BACK";
	public static String MENU = "KEYCODE_MENU";
	public static String POWER = "KEYCODE_POWER";

	public OperateAndroid(IDevice dev) {
		if (device == null) {

			while(true){
                IChimpDevice chimpDevice = new AdbChimpDevice(dev);
                device = (AdbChimpDevice) chimpDevice;
				manager = device.getManager();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				try {
                    //TODO 一个奇怪的bug，几乎每两次启动manager都会有一次失败
					manager.touchUp(1,1);
					Thread.sleep(1000);
                    manager.touchUp(1,1);
                    Thread.sleep(1000);
                    manager.touchUp(1,1);
					break;
				} catch (IOException e) {
					e.printStackTrace();
					manager.close();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}


            // try {
			// Collection<String> str = manager.listVariable();
			// for (Object ob : str) {
			// System.out.println(manager.getVariable(ob.toString()));
			// ;
			// }
			// } catch (IOException e) {
			// e.printStackTrace();
			// }
		}
	}

	public static OperateAndroid getOperateAndroid(IDevice dev) {
		if (oa == null) {
			oa = new OperateAndroid(dev);
		}
		return oa;

	}

	public void touch(int x, int y) {
		device.touch(x, y,
				com.android.chimpchat.core.TouchPressType.DOWN_AND_UP);
	}

	public void touch(int x, int y, TouchPressType type) {
		device.touch(x, y, type);
	}

	public void press(String str) {
		device.press(str, com.android.chimpchat.core.TouchPressType.DOWN_AND_UP);
	}

	public void press_DOWN(String str) {
		device.press(str, com.android.chimpchat.core.TouchPressType.DOWN);
	}

	public void press_UP(String str) {
		device.press(str, com.android.chimpchat.core.TouchPressType.UP);
	}

	public void drag(int startX, int startY, int endX, int endY, int time,
			int step) {
		device.drag(startX, startY, endX, endY, time, step);

	}

	public void type(char c) {
		device.type(Character.toString(c));
	}

	public void touchDown(int x, int y) throws Exception {
		manager.touchDown(x, y);
	}

	public void touchUp(int x, int y) throws Exception {
		manager.touchUp(x, y);
	}

	public void touchMove(int x, int y) throws Exception {
		manager.touchMove(x, y);
	}

	public int getScreenWidth() {
		return Integer.parseInt(device.getProperty("display.width"));
	}

	public int getScreenHeight() {
		return Integer.parseInt(device.getProperty("display.height"));
	}

	public static void injectListener(JPanel jps, final OperateAndroid oa){
		jps.addMouseListener(new MouseListener() {
			// 鼠标抬起
			public void mouseReleased(MouseEvent e) {
                int x = (int)(e.getX()*deviceWidthRatio);
                int y = (int)(e.getY()*deviceHeightRatio);
				try {
                    oa.touchUp(x,y);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				System.out.println("mouseReleased=>x:"+x+";y:"+y);
            }

			// 鼠标按下
			public void mousePressed(MouseEvent e) {
                int x = (int)(e.getX()*deviceWidthRatio);
                int y = (int)(e.getY()*deviceHeightRatio);
				try {
					oa.touchDown(x,y);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
                System.out.println("mousePressed=>x:"+x+";y:"+y);
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
			}
		});

		jps.addMouseMotionListener(new MouseMotionListener() {

			public void mouseMoved(MouseEvent e) {
			}

			// 鼠标按下拖动
			public void mouseDragged(MouseEvent e) {
                int x = (int)(e.getX()*deviceWidthRatio);
                int y = (int)(e.getY()*deviceHeightRatio);
				try {
					oa.touchMove(x,y);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
                System.out.println("mouseDragged=>x:"+x+";y:"+y);
			}
		});

		jps.addMouseWheelListener(new MouseWheelListener() {

			// 鼠标滚轮
			public void mouseWheelMoved(MouseWheelEvent e) {
				if (e.getWheelRotation() == 1) {
					oa.press("KEYCODE_DPAD_DOWN");
				} else if (e.getWheelRotation() == -1) {
					oa.press("KEYCODE_DPAD_UP");
				}
			}
		});
	}

    public static void removeListener(JPanel jps, final OperateAndroid oa){
        for(MouseListener mouseListener:jps.getMouseListeners()){
            jps.removeMouseListener(mouseListener);
        }
        for(MouseWheelListener mouseWheelListener:jps.getMouseWheelListeners()){
            jps.removeMouseWheelListener(mouseWheelListener);
        }
        for(MouseMotionListener mouseMotionListener:jps.getMouseMotionListeners()){
            jps.removeMouseMotionListener(mouseMotionListener);
        }
        oa.getManager().close();
    }

}
